package com.netease.qa.web.controller;

import com.netease.qa.biz.dto.ProjectDTO;
import com.netease.qa.biz.service.ProjectService;
import com.netease.qa.web.utils.ServiceResult;
import com.netease.qa.web.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/2/28
 */
@Controller
public class ProjectController {

    private static Logger log = LoggerFactory.getLogger(ProjectController.class);

    @Resource
    private ProjectService projectService;

    @RequestMapping(value = "/getNextProjectId", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getNextProjectId() {
        log.info("******getNextProjectId******");
        int nextProjectId = projectService.getNextProjectId();
        return ServiceResult.success(nextProjectId);
    }

    @RequestMapping(value = "/addProject", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult addProject(@RequestBody ProjectDTO projectDTO, HttpServletRequest request) {
        log.info("******addProject******");
        //校验项目编号
        ProjectDTO projectDTO1 = projectService.getProjectDetail(projectDTO.getProjectId());
        if (projectDTO1 != null) {
            return ServiceResult.successMsg("编号为" + projectDTO.getProjectId() + "的项目已经存在,请刷新新增页面!!!");
        }
        //校验团队
        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        if (!UserUtils.ifUserJoinedTeam(currentUserId, projectDTO.getTeamId())) {
            return ServiceResult.successMsg("您不属于该团队,没有权限给该团队添加项目!!!");
        }

        projectDTO.setCreator(currentUserId);
        projectService.addProject(projectDTO);
        return ServiceResult.successMsg("添加成功");
    }

    @RequestMapping(value = "/getAllProject", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getAllProject() {
        log.info("******getAllProject******");
        List<ProjectDTO> projectList = projectService.getAllProject();
        return ServiceResult.success(projectList);
    }

    @RequestMapping(value = "/getProjectDetail", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getProjectDetail(@RequestParam int projectId) {
        log.info("******getProjectDetail******");
        ProjectDTO projectDetail = projectService.getProjectDetail(projectId);
        return ServiceResult.success(projectDetail);
    }

    @RequestMapping(value = "/updateProject", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult updateProject(@RequestBody ProjectDTO projectDTO, HttpServletRequest request) {
        log.info("******updateProject******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        //只要是一个组的都有修改项目信息的权限
        if (!UserUtils.ifUserJoinedTeam(currentUserId, projectDTO.getTeamId())) {
            return ServiceResult.successMsg("您不属于该团队,没有权限更新项目信息");
        }
        projectDTO.setLastEditor(currentUserId);
        projectService.updateProject(projectDTO);
        return ServiceResult.successMsg("更新成功");
    }

    @RequestMapping(value = "/validateBeforeDeleteProject", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult validateBeforeDeleteProject(@RequestParam int projectId) {
        log.info("******validateBeforeDeleteProject******");

        String msg = projectService.validateBeforeDelete(projectId);
        return ServiceResult.successMsg(msg);
    }

    @RequestMapping(value = "/deleteProject", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult deleteProject(@RequestParam int projectId, HttpServletRequest request) {
        log.info("******deleteProject******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);

        ProjectDTO projectDTO = projectService.getProjectDetail(projectId);
        //只要是一个组的都有删除项目信息的权限
        if (!UserUtils.ifUserJoinedTeam(currentUserId, projectDTO.getTeamId())) {
            return ServiceResult.successMsg("您不属于该团队,没有权限删除该项目!!!");
        }
        projectService.deleteProject(projectId, currentUserId);
        return ServiceResult.successMsg("删除成功");
    }

    @RequestMapping(value = "/getProjectListInCondition", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getProjectListInCondition(HttpServletRequest request) {
        log.info("******getAllProject******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);

        List<ProjectDTO> projectList = projectService.getUserRelatedProjectList(currentUserId);
        return ServiceResult.success(projectList);
    }

    @RequestMapping(value = "/confirmEditProjectPermission", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult confirmEditProjectPermission(@RequestParam int projectId, HttpServletRequest request) {
        log.info("******confirmEditProjectPermission******");

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        boolean result = projectService.confirmEditProjectPermission(projectId, currentUserId);
        if (result) {
            return ServiceResult.successMsg("验证通过");
        }
        return ServiceResult.successMsg("您不属于该团队,没有权限编辑项目信息");
    }

}
